package gameObject
{
	import flash.events.Event;
	import flash.net.Responder;
	
	import gameObject.resources3D.GameMesh;
	import gameObject.resources3D.ResourceBase;
	import gameObject.resources3D.RobotResource;
	
	import ui.component.EquipIcon;
	
	/**
	 * 机械库，商店中的高模机甲
	 * 
	 */
	public class RobotPart extends GameObjectBase
	{
		private var _id:int;
		private var _inc:int;
		public var hp:int=0;
		public var dur:int=0;
		public var wep_m:Vector.<int> = new Vector.<int>;
		public var wep_s:Vector.<int> = new Vector.<int>;
		private var _equip:Boolean = false;
		
		//-----------------------------------
		private var _rName:String;
		private var 	_level:int;
		private var _cost:int;
		private var 	_def:int;
		private var 	_speed:Number;
		private var _con_m:uint;
		private var _con_s:uint;
		private var	_maxhp:int;
		private var 	_maxdur:int;
		
		//-----------------------------------
		private var _resComplete:Boolean = false;
		private var _icon:EquipIcon;
		
		////////////////////////////////////////////////////

		public function get id():int
		{
			return _id;
		}

		public function get con_s():uint
		{
			return _con_s;
		}

		public function get con_m():uint
		{
			return _con_m;
		}

		public function get equip():Boolean
		{
			return _equip;
		}

		public function set equip(value:Boolean):void
		{
			_equip = value;
			if(_icon)_icon.equip = value;
		}

		public function get icon():EquipIcon
		{
			return _icon;
		}

		public function get inc():int
		{
			return _inc;
		}

		override public function get name():String
		{
			return _rName;
		}
		public function get level():int
		{
			return _level;
		}
		public function get cost():int
		{
			return _cost;
		}
		public function get sellCost():int
		{
			var val:int;
			val = 0.6*cost;
			val *= dur/maxdur;
			val *= hp/maxhp;
			
			return val;
		}
		public function get def():int
		{
			return _def;
		}
		public function get speed():Number
		{
			return _speed;
		}
		public function get maxhp():int
		{
			return _maxhp;
		}
		public function get maxdur():int
		{
			return _maxdur;
		}

		////////////////////////////////////////////////////
		/**
		 * @param ID
		 * 编号申请后就没得变了
		 * @param inc
		 * 强化等级
		 */		
		public function RobotPart(ID:int,inc:int)
		{
			_id = ID;
			_inc = inc;
			
			_icon = new EquipIcon(1,_id,_inc,116,116);
			Global.phpServer.call("robotServer.getRobotVals", new Responder(gotValues),ID,inc);
		}
		
		override public function dispose():void
		{
			super.dispose();
			if(_resource)_resource.dispose();
			if(_controller)_controller.dispose();
		}
		
		public function displayModel():void
		{
			if(_resource)addChild(_resource)
			else
			{
				addEventListener(Event.COMPLETE,displayModelEvent);
				function displayModelEvent(e:Event):void
				{
					addChild(_resource);
				}
			}
		}
		
		public function hideModel():void
		{
			if(_resource)
			{
				//if(_resource.legMesh && _resource.legMesh.animator)_resource.legMesh.animator.stop();
				//if(_resource.bodyMesh && _resource.bodyMesh.animator)_resource.bodyMesh.animator.stop();
				addChild(_resource);//免得没显示模型时使用这个方法报错
				removeChild(_resource);
			}
		}
		
		////////////////////////////////////////////////////
		private function gotValues(obj:Object):void
		{
			_rName = obj.name;
			_level = obj.level
			_cost = obj.cost;
			_def = obj.def;
			_speed = obj.speed;
			_con_m = obj.con_m;
			_con_s = obj.con_s;
			_maxhp =obj.maxhp;
			_maxdur = obj.maxdur;
			
			_icon.name = _rName;
			_icon.equip = _equip;
			
			_resource = new RobotResource(_id,GameMesh.LOD_HIGH);
			_resource.addEventListener(Event.COMPLETE,resourceLoaded);
			_resource.load();
		}
		
		private function resourceLoaded(e:Event):void
		{
			_resComplete = true;
			dispatchEvent(new Event(Event.COMPLETE));
		}
		
	}
}